package ch01.thread;

/**
 *  单例设计模式：懒汉模式
 * Created by zc on 2016/12/6.
 */
public class SingletonTest {

    public static void main(String[] args){
        Custom t = new Custom();
        Thread thread1 = new Thread(t, "Thread1");
        Thread thread2 = new Thread(t, "Thread2");
        Thread thread3 = new Thread(t, "Thread3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class Custom implements Runnable {
    @Override
    public void run() {
        Singleton singleton = Singleton.getInstance();
        System.out.println("Thread.currentTheread(): " + Thread.currentThread().getName() +" " + singleton);
    }
}

class Singleton {
    private static Singleton singleton = null;
    public static Singleton getInstance(){
        if (singleton == null) {
            synchronized ("lock"){
                if(singleton==null){
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}
